package com.ah.suams.util;

import java.util.Random;

/**
 * 随机工具类
 * 生成四位随机数字
 * 生成四位随机大小写英文字母
 */
public class RandomUtil {

    private static final Random RANDOM_UTIL = new Random();

    //数字
    private static final String[] NUMBER = {"0","1","2","3","4","5","6","7","8","9"};

    //小写字母
    private static final String[] STR_SM = {
            "q","w","e","r","t","y","u","i","o","p",
            "a","s","d","f","g","h","j","k","l",
            "z","x","c","v","b","n","m"
    };

    //大写字母
    private static final String[] STR_LG = {
            "Q","W","E","R","T","Y","U","I","O","P",
            "A","S","D","F","G","H","J","K","L",
            "Z","X","C","V","B","N","M"
    };

    //获取一个随机数,[0,end)
    private static int getRandomNumber(int size){
        return RANDOM_UTIL.nextInt(size);
    }

    //获取一个随机数字字符
    private static String getRandomNumberStr(){
        return NUMBER[getRandomNumber(NUMBER.length)];
    }

    //获取一个随机小写英文字符
    private static String getRandomSmLetter(){
        return STR_SM[getRandomNumber(STR_SM.length)];
    }

    //获取一个随机大写英文字符
    private static String getRandomLgLetter(){
        return STR_LG[getRandomNumber(STR_LG.length)];
    }

    //获取一个随机英文字符
    //0-->小写
    //1-->大写
    private static String getRandomLetter(){
        int d = RANDOM_UTIL.nextInt(2);

        if(d == 0){
            return getRandomSmLetter();
        }
        return getRandomLgLetter();
    }

    //4位
    private static final int COUNT_4 = 4;
    //6位
    private static final int COUNT_6 = 6;
    //纯数字字符
    private static final String ALL_NUMBER = "N";
    //纯小写字母字符
    private static final String S_LETTER = "SL";
    //纯大写写字母字符
    private static final String L_LETTER = "LL";
    //大小写字母字符
    private static final String SAL_LETTER = "SAL";

    //获取随机字符
    private static String getRandomText(int count,String diff){
        StringBuilder builder = new StringBuilder();

        for(int i = 0;i < count;i++){
            if(ALL_NUMBER.equals(diff)){
                //数字
                builder.append(getRandomNumberStr());
            }else if(S_LETTER.equals(diff)){
                //小写字母
                builder.append(getRandomSmLetter());
            }else if(L_LETTER.equals(diff)){
                //大写字母
                builder.append(getRandomLgLetter());
            }else if(SAL_LETTER.equals(diff)){
                //大小写
                builder.append(getRandomLetter());
            }
        }

        return builder.toString();
    }


    //获取一个随机字符
    public static String getRandomStr(){
        int r = RANDOM_UTIL.nextInt(2);
        if(r == 0){
            return getRandomNumberStr();
        }
        return getRandomLetter();
    }

    //获取四个随机验证字母，区别大小写
    public static String getRandomText(){
        return getRandomText(COUNT_4,SAL_LETTER);
    }

    //获取四个随机小写字母
    public static String getRandomSmText(){
        return getRandomText(COUNT_4,S_LETTER);
    }

    //获取四个随机大写字母
    public static String getRandomLgText(){
        return getRandomText(COUNT_4,L_LETTER);
    }

    //获取四个随机数字
    public static String getRandomNumberText(){
        return getRandomText(COUNT_4,ALL_NUMBER);
    }

}
